﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using QLHD.DAL;
using QLHD.BLL;

namespace QLHD
{
    public partial class frmMaNhapXuat : Form
    {
        public frmMaNhapXuat() { }
        public frmMaNhapXuat(frmMain form)
        {
            InitializeComponent();
            MdiParent = form;
        }

        private void frmMaNhapXuat_Load(object sender, EventArgs e)
        {
            dgvMaNX.DataSource = MaNX_DB.SelectAllMaNX();
            ReadOnlyTextbox();
            DisplayData(0);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmMaNhapXuat_Load(sender, e);
            btnThem.Text = "Thêm";
            btnSua.Text = "Sửa";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Thêm")
            {
                btnThem.Text = "Lưu";
                NotReadOnlyTextbox();
                txtDiengiai.Clear();
                txtMaNX.Clear();
                txtMaNX.Focus();
            }
            else
            {
                try
                {
                    MaNX item = new MaNX(txtMaNX.Text, txtDiengiai.Text);
                    MaNX_DB.Insert(item);
                    btnThem.Text = "Thêm";
                    frmMaNhapXuat_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }                
            }
        }
        private void ReadOnlyTextbox()
        {
            txtMaNX.ReadOnly = true;
            txtDiengiai.ReadOnly = true;           
        }
        private void NotReadOnlyTextbox()
        {
            txtMaNX.ReadOnly = false;
            txtDiengiai.ReadOnly = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            MaNX_DB.Delete(txtMaNX.Text);
            frmMaNhapXuat_Load(sender, e);
        }
        private void DisplayData(int index)
        {
            txtMaNX.Text = dgvMaNX[0, index].Value.ToString();
            txtDiengiai.Text = dgvMaNX[1, index].Value.ToString();
        }

        private void dgvMaNX_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DisplayData(e.RowIndex);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text =="Sửa")
            {
                btnSua.Text = "Lưu";
                txtDiengiai.ReadOnly = false;
            }
            else
            {
                btnSua.Text = "Sửa";
                MaNX item = new MaNX(txtMaNX.Text, txtDiengiai.Text);
                MaNX_DB.Update(txtMaNX.Text, item);
                frmMaNhapXuat_Load(sender, e);
            }
        }
    }
}
